package com.lzh.shijicloudstack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.shijicloudstack.mapper.IngredientCategoryMapper;
import com.lzh.shijicloudstack.mapper.IngredientMapper;
import com.lzh.shijicloudstack.model.dto.ingredient.AddIngredientDto;
import com.lzh.shijicloudstack.model.dto.ingredient.EditIngredientDto;
import com.lzh.shijicloudstack.model.dto.ingredient.IngredientPageDto;
import com.lzh.shijicloudstack.model.entity.Ingredient;
import com.lzh.shijicloudstack.model.entity.IngredientCategory;
import com.lzh.shijicloudstack.model.vo.ingredient.IngredientVO;
import com.lzh.shijicloudstack.service.IngredientService;
import com.lzh.shijicloudstack.template.PageAbstractTemplate;
import com.lzh.shijicloudstack.utils.BeanCopyUtils;
import com.lzh.shijicloudstack.utils.MinIOUtils;
import io.minio.ObjectWriteResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author luzhiheng
 * @date 2025-01-25
 */
@Service
public class IngredientServiceImpl extends ServiceImpl<IngredientMapper, Ingredient> implements IngredientService {

    @Resource
    private IngredientCategoryMapper ingredientCategoryMapper;

    @Override
    public Map<String, Object> pageList(IngredientPageDto dto) {
        Map<String, Object> otherQueryMap = new HashMap<>();

        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(dto.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pageList = baseMapper.pageList(this.queryMap);

                List<IngredientCategory> ingredientCategoryList = ingredientCategoryMapper.selectList(null);
                Map<Long, IngredientCategory> categoryMap = ingredientCategoryList.stream().collect(Collectors.toMap(IngredientCategory::getCategoryId, e -> e));
                for (Map<String, Object> map : pageList) {
                    Object categoryId = map.get("category_id");
                    if (categoryId != null) {
                        IngredientCategory category = categoryMap.get((Long) categoryId);
                        if (category != null) {
                            map.put("categoryName", category.getName());
                        }
                    }
                }
                return pageList;
            }
        };
        return template.queryPage();
    }

    @Override
    public Boolean add(AddIngredientDto dto) {
        Ingredient ingredient = BeanCopyUtils.copyBean(dto, Ingredient.class);
        if (ingredient != null) {
            return save(ingredient);
        }
        return false;
    }

    @Override
    public Boolean edit(EditIngredientDto dto) {
        Ingredient ingredient = BeanCopyUtils.copyBean(dto, Ingredient.class);
        if (ingredient != null) {
            return updateById(ingredient);
        }
        return false;
    }

    @Override
    public IngredientVO findDetail(Long id) {
        Ingredient ingredient = baseMapper.selectById(id);
        IngredientVO ingredientVO = BeanCopyUtils.copyBean(ingredient, IngredientVO.class);

        List<IngredientCategory> ingredientCategoryList = ingredientCategoryMapper.selectList(null);
        Map<Long, IngredientCategory> categoryIdMap = ingredientCategoryList.stream().collect(Collectors.toMap(IngredientCategory::getId, e -> e, (k1, k2) -> k1));

        List<Long> categoryShowIdList = new ArrayList<>();
        Long categoryId = ingredient.getCategoryId();
        if (categoryId != null) {
            categoryShowIdList.add(0, categoryId);
            IngredientCategory category = categoryIdMap.get(categoryId);
            if (category != null) {
                Long parentId = category.getParentId();
                while (parentId != -1) {
                    categoryShowIdList.add(0, parentId);
                    parentId = categoryIdMap.get(parentId).getParentId();
                }
            }
        }
        ingredientVO.setCategoryShowIdList(categoryShowIdList);
        return ingredientVO;
    }
}
